package com.stephenone.zhdaily.uitl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2018/1/31.
 */

public class TimeUtil {
    public static String timestampToFormattedTime(long timestamp ){
        SimpleDateFormat sdf=new SimpleDateFormat("MM-dd HH:mm");//这个是你要转成后的时间的格式
        return sdf.format(new Date(timestamp));
    }

    public static String timestampToFormattedTime2(long timestamp ){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");//这个是你要转成后的时间的格式
        return sdf.format(new Date(timestamp));
    }

    public static Date stringToDate(String dateStr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = null;
        try {
            date=sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String dateToString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    public static String beforeThis(String current){
        Date currentDate = stringToDate(current);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return dateToString(calendar.getTime());
    }

    public static String afterThis(String current){
        Date currentDate = stringToDate(current);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return dateToString(calendar.getTime());
    }
}
